package com.yaozheng.vocationaltraining.fragment.set_up;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.TextView;

import com.yaozheng.vocationaltraining.BuildConfig;
import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.dialog.ask.ConfirmCancelAskDialog;
import com.yaozheng.vocationaltraining.fragment.BaseFragment;
import com.yaozheng.vocationaltraining.iview.set_up.ICheckingVersionView;
import com.yaozheng.vocationaltraining.service.impl.set_up.CheckingVersionServiceImpl;
import com.yaozheng.vocationaltraining.service.set_up.CheckingVersionService;
import com.yaozheng.vocationaltraining.utils.ControlsUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

/**
 * 检查版本更新(检查更新)
 */
@EFragment(R.layout.fragment_checking_version_update)
public class CheckingVersionUpdateFragment extends BaseFragment implements ICheckingVersionView {
    @ViewById
    TextView headTitleText, chechVersionExplanationView;
    @Bean(CheckingVersionServiceImpl.class)
    CheckingVersionService checkingVersionService;
    //    int appVersionCode;
    String appVersionName;
    String appverUrl;
    ConfirmCancelAskDialog confirmCancelAskDialog;

    @AfterViews
    public void initView() {
        checkingVersionService.init(this);
        headTitleText.setText("检测更新");
//        appVersionCode = ControlsUtils.getAppVersionCode(getBaseActivity());
        appVersionName = BuildConfig.VERSION_NAME; // ControlsUtils.getAppVersionName(getBaseActivity());
        chechVersioClick();
    }

    /**
     * 检查版本
     */
    @Click(R.id.chechVersionButtonView)
    public void chechVersioClick() {
        showOperatingProgressDialog("检测版本中...");
        checkingVersionService.checkingVersion();
    }

    @UiThread
    @Override
    public void checkingVersionSuccess(JSONObject jsonObject) {
        cancelOperatingProgressDialog();
        JSONObject dataJsonObject = TypeUtils.getJsonObject(jsonObject, "data");
        String version = TypeUtils.getJsonString(dataJsonObject, "version");
        appverUrl = TypeUtils.getJsonString(dataJsonObject, "package");
        chechVersionExplanationView.setVisibility(View.VISIBLE);
        if (version.equals(appVersionName)) {
            chechVersionExplanationView.setText("当前已是最新版本");
        } else {
            chechVersionExplanationView.setText("发现新的版本：" + version);

            if (confirmCancelAskDialog == null) {
                confirmCancelAskDialog = new ConfirmCancelAskDialog(this);

            }
            confirmCancelAskDialog.show();
            confirmCancelAskDialog.setTitleAndroidContent("版本更新", "发现新的版本是否更新？");
        }

    }

    @UiThread
    @Override
    public void checkingVersionError(String error) {
        cancelOperatingProgressDialog();
        alertMessage(error);
    }

    /**
     * 确认
     */
    public void confirmCancelAskDialogDetermineClick() {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(appverUrl));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    @Override
    public void onDestroyView() {
        if (confirmCancelAskDialog != null && confirmCancelAskDialog.isShowing()) {
            confirmCancelAskDialog.cancel();
        }
        super.onDestroyView();
    }
}
